package com.coocaa.ops.admin.v1.policygroup.validator;

import com.coocaa.ops.admin.base.exception.AdminBizException;
import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.policygroup.model.HomeMatchQueryModel;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.core.biz.policygroup.constant.PolicyGroupCategoryConstants;
import com.coocaa.ops.common.core.biz.policygroup.entity.PolicyGroupInfoEntity;
import lombok.experimental.UtilityClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * @author bijiahao
 * @description
 * @date 2020/3/13 14:13
 */
@Component
public class PolicyGroupValidator {
    private static CommonLanguageProperties commonLanguageProperties;

    public static void validBeforeSave(PolicyGroupInfoEntity policyGroupInfoEntity) {
        if (ResourceStatusValidator.isOnline(policyGroupInfoEntity.getStatus())) {
            throw new AdminBizException(commonLanguageProperties.getNotAudit());
        }
        Assert.hasText(policyGroupInfoEntity.getPolicyGroupName(), commonLanguageProperties.getPolicyGroupInfo() + commonLanguageProperties.getName() + commonLanguageProperties.getNotNull());
        Assert.notNull(policyGroupInfoEntity.getPolicyGroupCategory(), commonLanguageProperties.getPolicyGroupInfo() + commonLanguageProperties.getResourceType() + commonLanguageProperties.getNotNull());
        Assert.notEmpty(policyGroupInfoEntity.getRlsModelChipList(), "model、chip" + commonLanguageProperties.getNotNull());
        if (PolicyGroupCategoryConstants.isTest(policyGroupInfoEntity.getPolicyGroupCategory())) {
            Assert.hasText(policyGroupInfoEntity.getMac(), "mac" + commonLanguageProperties.getNotNull());
        }
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        PolicyGroupValidator.commonLanguageProperties = commonLanguageProperties;
    }

    public static void valid(HomeMatchQueryModel model) {
        Assert.hasText(model.getModel(), "model " + commonLanguageProperties.getNotNull());
        Assert.hasText(model.getChip(), "chip " + commonLanguageProperties.getNotNull());
        Assert.hasText(model.getPattern(), "pattern " + commonLanguageProperties.getNotNull());
    }
}
